import com.smartfoxserver.v2.SmartFox;
import com.smartfoxserver.v2.core.SFSEvent;
import com.smartfoxserver.v2.requests.LoginRequest;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

private var sfs:SmartFox
		
public function init():void{
	// Create an instance of the SmartFox class
	sfs = new SmartFox(true);
			
	// Turn on the debug feature
//	sfs.debug = true
			
	// Add SFS2X event listeners
	sfs.addEventListener(SFSEvent.CONNECTION, onConnection)
	sfs.addEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost)
	sfs.addEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, onConfigLoadSuccess)
	sfs.addEventListener(SFSEvent.CONFIG_LOAD_FAILURE, onConfigLoadFailure)
		
		
	// Add event listeners
/*
	sfs.addEventListener(SFSEvent.LOGIN_ERROR, onLoginError);
	sfs.addEventListener(SFSEvent.LOGIN, onLogin);
	sfs.addEventListener(SFSEvent.ROOM_JOIN_ERROR, onRoomJoinError);
	sfs.addEventListener(SFSEvent.ROOM_JOIN, onRoomJoin);
	sfs.addEventListener(SFSEvent.PUBLIC_MESSAGE, onPublicMessage);
	sfs.addEventListener(SFSEvent.USER_ENTER_ROOM, onUserEnterRoom);
	sfs.addEventListener(SFSEvent.USER_EXIT_ROOM, onUserExitRoom);
	sfs.addEventListener(SFSEvent.USER_COUNT_CHANGE, onUserCountChange);
	sfs.addEventListener(SFSEvent.ROOM_ADD, onRoomAdd);
	sfs.addEventListener(SFSEvent.ROOM_REMOVE, onRoomRemove);
	sfs.addEventListener(SFSEvent.ROOM_CREATION_ERROR, onRoomCreationError);
*/
		
			
	// Connect button listener
/*	bt_connect.addEventListener(MouseEvent.CLICK, onBtConnectClick)
			
	dTrace("SmartFox API: " + sfs.version)
	dTrace("Click the CONNECT button to start...")	*/
}  
		

/**
 * Login user.
 */
private function login():void{
	var username:String = loginPanel.textImputUsername.text;
	var request:LoginRequest = new LoginRequest(username);
	sfs.send(request);
}


//---------------------------------
// User interaction event handlers
//---------------------------------

/**
 * Login button click event listener.
 * Establish a connection to SmartFoxServer (if necessary) and perform login.
 */
public function onLoginBtClick():void{
	// Set login panel state
	loginPanel.isConnecting = true;
	
	// Clear any previous message in the login panel
	loginPanel.ta_error.text = "";
	
	// Check if connection is already available
	// (this can happen in case of login error, for example)
	if (!sfs.isConnected){
		// If this is the first time we are attempting the connection,
		// we have to load the external configuration file containing the connection details (ip, port, zone)
		// We know if this is the first connection by checking the SmartFox.config property (not null if configuration is already loaded)
		if (sfs.config == null)
			sfs.loadConfig("config/sfs-config.xml", true);
		else
			sfs.connect();
	}
	else
		login();
}


/**
 * Disconnect button click event listener.
 * Disconnect from SmartFoxServer.
 */
private function onDisconnectBtClick():void{
	sfs.disconnect();
}


/**
 * Room list selection change event listener.
 * Join the selected room.
 */

/*
private function onRoomListChange():void{
	if (ls_rooms.selectedIndex > -1){
		// Join selected room
		joinRoom(ls_rooms.selectedItem.name);
	}
	else{
		// Re-select last joined room
		// This is required in case user deselects currently selected item by holding the CMD (Mac) or CTRL (Windows) key
		ls_rooms.selectedIndex = lastJoinedRoomIndex;
	}
}
*/














		private function onBtConnectClick(evt:Event):void{
			// Load the default configuration file, config.xml
			sfs.loadConfig()
		}
		
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// SFS2X event handlers
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function onConnection(evt:SFSEvent):void{
			if (evt.params.success){
				dTrace("Connection Success!")
			}
			else{
				dTrace("Connection Failure: " + evt.params.errorMessage)
			}
		}
		
		private function onConnectionLost(evt:SFSEvent):void{
			dTrace("Connection was lost. Reason: " + evt.params.reason)
		}
		
		private function onConfigLoadSuccess(evt:SFSEvent):void{
			dTrace("Config load success!")
			dTrace("Server settings: " + sfs.config.host + ":" + sfs.config.port)
		}
		
		private function onConfigLoadFailure(evt:SFSEvent):void{
			dTrace("Config load failure!!!")
		}
		
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// Utility methods
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function dTrace(msg:String):void{
//			ta_debug.text += "-- " + msg + "\n";
		}  
